﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Security.Cryptography;
using System.Text;

public partial class admin_PersonInfo : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (!checkLoginAdmin())
            {
                ClientScript.RegisterStartupScript(typeof(Page), "alert", "<script> alert('You have to login as Admin to access this page!');</script>");
                Response.Redirect("Login.aspx?prePage=personInfo");
            }
            LoadData();
        }
    }
    // check login
    public bool checkLoginAdmin()
    {
        // check session
        if (Session["username"] != null)
        {
            return true;
        }
        else
        {
            // check cookie 
            if (Request.Cookies["username"] != null)
            {
                Session["username"] = Request.Cookies["username"].Value;
                Session["fullname"] = Request.Cookies["fullname"].Value;
                return true;
            }

        }
        return false;
    }

    private void LoadData()
    {
        DataTable dt = AdminManagement.GetAdminByUser(Session["UserName"].ToString()).Tables["Admin"];
        if (dt.Rows.Count > 0)
        {
            DataList1.DataSource = dt;
            DataList1.DataBind();

            lblMessage.Text = "";

            txtEmail.Text = dt.Rows[0][AdminManagement.EMAIL].ToString();
            txtAdminName.Text = dt.Rows[0][AdminManagement.ADMINNAME].ToString();
            txtTel.Text = dt.Rows[0][AdminManagement.TEL].ToString();
        }

        MultiView1.ActiveViewIndex = 0;
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        MultiView1.ActiveViewIndex = 1;
    }
    protected void LinkButton3_Click(object sender, EventArgs e)
    {
        MultiView1.ActiveViewIndex = 2;
    }
    protected void btnOK_Click(object sender, EventArgs e)
    {
        //Check old password:
        byte[] input = Encoding.ASCII.GetBytes(txtOldPass.Text);
        MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
        byte[] output = md5.ComputeHash(input);
        string oldPassEncoding = Encoding.ASCII.GetString(output);
        if (!AdminManagement.CheckUser(Session["UserName"].ToString(), oldPassEncoding))
            lblMessage.Text = "Old password invalid!";
        else
        {
            //Change password:
            input = Encoding.ASCII.GetBytes(txtNewPass.Text);
            md5 = new MD5CryptoServiceProvider();
            output = md5.ComputeHash(input);
            string newPassEncoding = Encoding.ASCII.GetString(output);
            if (AdminManagement.ChangePass(Session["UserName"].ToString(), newPassEncoding))
            {
                LoadData();
                MultiView1.ActiveViewIndex = 0;
            }
        }
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        MultiView1.ActiveViewIndex = 0;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        //Update session
        Session["fullname"] = txtAdminName.Text;
        //Update cookie
        //HttpCookie cookie = new HttpCookie("fullname", txtAdminName.Text);
        //cookie.Expires = DateTime.Today.AddDays(15);
        //Response.Cookies.Add(cookie);

        //Update
        if (AdminManagement.EditAdmin(txtAdminName.Text, txtEmail.Text, txtTel.Text,Session["UserName"].ToString()))
        {
            LoadData();
            MultiView1.ActiveViewIndex = 0;
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        MultiView1.ActiveViewIndex = 0;
    }
}